<?php
	include_once(dirname(__DIR__) . "/include/dbconnection.php");
	
	class Frontpage {
		private $forums = array();
		
		private $html;
		
		function Frontpage() {
			$this -> loadForumsFromDatabase();
			$this -> generateFrontpageHTML();
		}
		
		private function loadForumsFromDatabase() {
			$query = "SELECT forum_id, upper_forum_id, name, description, closed, no_threads FROM t_forum WHERE upper_forum_id IS NULL";
			$result = mysql_query($query) or die("Invalid query");
			$row_count = mysql_num_rows($result);
			
			for($row_counter = 0; $row_counter < $row_count; $row_counter++) {
				$row = mysql_fetch_assoc($result);
				$this -> forums[] = $row;
			}
			
			mysql_free_result($result);
		}
		
		private function generateFrontpageHTML() {
			foreach($this -> forums as $forum) {
				$forum_class = "forum";
				
				if($forum['closed']) {
					$forum_class .= " closed";
				}
				
				$this -> html .=
					"<div class=\"". $forum_class ."\">".
						"<p><a href=\"./index.php?forum_id=". $forum['forum_id'] ."\">". $forum['name'] ."</a><br />".
							"&nbsp;&nbsp;<i>". $forum['description'] ."</i>".
						"</p>".
					"</div>";
			}
		}
		
		public function show() {
			echo $this -> html;
		}
	}
?>